package com.luntan.service.impl;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.luntan.domain.HuiTie;
import com.luntan.domain.MainTie;
import com.luntan.service.HuiTieService;
import com.luntan.util.web.Page;

/**
 * 帖、回帖的业务逻辑层（实现类）
 * 
 */
public class HuiTieServiceImpl extends BaseServiceImpl<HuiTie, Integer> implements HuiTieService {

	/**
	 * 保存
	 */
	@Override
	public boolean addSave(HuiTie huiTie) {
		boolean flag = false;
		try {
			this.baseDAO.saveEntity(huiTie);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	/**
	 * 保存修改
	 */
	@Override
	public boolean updateSave(HuiTie huiTie) {
		boolean flag = false;
		try {
			HuiTie temp = this.baseDAO.getEntity(HuiTie.class, huiTie.getHid());
			temp.setHcontent(huiTie.getHcontent());
			this.baseDAO.updateEntity(temp);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	/**
	 * 取出修改
	 */
	@Override
	public HuiTie update(Integer id) {
		try {
			HuiTie huiTie = this.baseDAO.getEntity(HuiTie.class, id);
			return huiTie;
		} catch (Exception ex) {
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	/**
	 * 删除
	 */
	@Override
	public boolean delete(Integer id) {
		boolean flag = false;
		try {
			StringBuilder hsql = new StringBuilder();
			hsql.append("FROM HuiTie AS m WHERE m.hid=").append(id);
			List<HuiTie> list = this.baseDAO.findEntity(hsql.toString());
			this.baseDAO.removeAllEntity(list);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}
	
	/**
	 * 带条件获取列表
	 */
	@Override
	public Page<HuiTie> getList(int page, int limit, HuiTie huiTie) {
		StringBuilder hsql = new StringBuilder();
		StringBuilder where = new StringBuilder(" WHERE 1=1 ").append(this.getWhere(huiTie));
		hsql.append("FROM HuiTie AS m ").append(where);

		try {
			Page<HuiTie> list = this.baseDAO.search(hsql.toString(), page, limit);
			return list;
		} catch (Exception ex) {
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	/**
	 * 获取列表
	 */
	@Override
	public List<HuiTie> getList() {
		StringBuilder hsql = new StringBuilder();
		hsql.append("FROM HuiTie AS m ");
		try {
			List<HuiTie> list = this.baseDAO.findEntity(hsql.toString());
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			Logger.getLogger(HuiTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

}
